package cn.chencaiju.day08;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import org.junit.Test;

public class StackTest {
	public boolean isLegalRegularity(char[] ch) {
		Map<Character,Character> regularity=new HashMap<>();
		regularity.put('(', ')');
		regularity.put('[', ']');
		regularity.put('{', '}');
		Stack<Character> s=new Stack<Character>();
		for(int i=0;i<ch.length;i++) {
			if(ch[i]=='('||ch[i]=='['||ch[i]=='{') {
				s.push(ch[i]);
			}else {
				if(s.isEmpty())
					return false;
				char top=(char) s.pop();
				if(ch[i]!=regularity.get(top))
					return false;
			}
		}
		return s.isEmpty();
	}
	public static boolean isValid(String str) {
		Map<Character, Character> map = new HashMap<>();
		map.put('(', ')');
		map.put('[', ']');
		map.put('{', '}');

		// 包装类型
		// int -> Integer
		// double -> Double
		// char -》 Character
		Stack<Character> stack = new Stack<>();
		
		for (int i = 0; i < str.length(); i++) {
			char c = str.charAt(i);
			
			if (c == '(' || c == '[' || c == '{') {
				stack.push(c);
			} else {
				if(stack.isEmpty())
					return false;
				char top = stack.pop();		// 左边的
				char right = map.get(top);
				if (right != c) {	// 左边的和右边的匹配
					return false;
				}
			}
		}

		return stack.isEmpty();
	}
	@Test public void testStack() {
		String[] str= {
				"{([]())}",
				"[(()()[])]",
				"[(])",
				"{{)}",
				"{{[]}",
				"",
				"{{[[",
				"{",
				"}"
		};
		for(String temp:str) {
			System.out.println(isLegalRegularity(temp.toCharArray())+"\t"+isValid(temp));
		}
	}
}
